package com.roc.dao.impl;

import org.hibernate.NonUniqueResultException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.roc.dao.DaoException;
import com.roc.dao.ITerminalSnBatchDao;
import com.roc.pojo.YjPosBatchInfo;

@Repository
public class TerminalSnBatchDaoImpl extends BaseDaoImpl implements ITerminalSnBatchDao {


	@Override
	public YjPosBatchInfo queryYjPosBetchInfo(String serialNum,String posBrand) throws DaoException{
		YjPosBatchInfo result = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			Query query = session.createQuery("from YjPosBatchInfo y where y.serialNum = :serialNum and y.posBrand = :posBrand");
			query.setString("serialNum", serialNum);
			query.setString("posBrand", posBrand);
			result = (YjPosBatchInfo) query.uniqueResult();
		}catch(NonUniqueResultException nure){
			logger.debug("查询YjPosBatchInfo不唯一", nure);
			throw new DaoException("查询终端流水号不唯一");
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("查询终端流水号不唯一,查询出错");
		}
		return result;
	}

	@Override
	public void deletePOSBatchByBatchNo(String batchNo) throws DaoException {
		int i = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			if(batchNo!=null &&!"".equals(batchNo)){
				String sql = "delete from yj_pos_batch p where p.BATCH_NO='" + batchNo + "'";
				Query query = session.createSQLQuery(sql);
				i = query.executeUpdate();
			}
			else{
				throw new DaoException("删除SN号段出错");
			}	
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("删除SN号段出错");
		}
	}
	
	@Override
	public void deletePOSBatchInfoByBatchNo(String batchNo) throws DaoException {
		int i = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			if(batchNo!=null &&!"".equals(batchNo)){
				String sql = "delete from yj_pos_batch_info p where p.BATCH_NO='" + batchNo + "'";
				Query query = session.createSQLQuery(sql);
				i = query.executeUpdate();
			}
			else{
				throw new DaoException("删除SN号段信息出错");
			}	
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("删除SN号段信息出错");
		}
	}
}
